package designpattern.proxy.findGirlFriend.dynamic;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 刘牌
 * @Date: 2021/12/17/14:12
 * @Description:
 */
public class MikeyCglibProxyTeacher implements MethodInterceptor {

    private final Enhancer enhancer = new Enhancer();

    public Object getProxy(Class<?> clazz){
        if (clazz == null){
            throw new RuntimeException("get object instance the param does not null");
        }
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("mikey老师：我现在开始帮广大单身网友找女朋友");
        Object response = methodProxy.invokeSuper(o, objects);
        System.out.println("mikey老师：为单身网友找到女朋友");
        return response;
    }
}
